package world.liquor.controller;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import world.liquor.domain.BeerAssessment;
import world.liquor.domain.BeerInfo;
import world.liquor.service.BeerAssessmentService;
import world.liquor.service.BeerSearchService;
import world.member.domain.Member;

@Controller
public class BeerSearchController {
	
	private BeerSearchService service;
	private BeerAssessmentService service2;
	@Autowired
	public BeerSearchController(BeerSearchService service, BeerAssessmentService service2){
		this.service=service;
		this.service2=service2;
	}
	
	//메인페이지 검색창
	@RequestMapping("/liquor/beer_search_index.do")
	public String searchBeerIndex(String value, ModelMap map, HttpSession session) throws SQLException{
		ArrayList<BeerInfo> list = service.searchBeerByMultipleCategory(value);
		ArrayList<BeerAssessment> list_as = common(session);
		map.addAttribute("result", list);		
		map.addAttribute("result2", list_as);
		return "/beer/beer_search.jsp";
	}
	//맥주검색 페이지 로딩시
	@RequestMapping("/liquor/beer_search_page.do")
	public String beerSearchPageLoading(ModelMap map, HttpSession session) throws SQLException{
		ArrayList<BeerInfo> list = service.selectAllBeer();
		ArrayList<BeerAssessment> list_as = common(session);
		
		map.addAttribute("result", list);
		map.addAttribute("result2", list_as);
		
		return "/beer/beer_search.jsp";
	}
	//맥주검색(이름-한/영 or 나라 or 제조사)
	@RequestMapping("/liquor/beer_search.do")
	public String searchBeerByMultipleCategory(String value, ModelMap map, HttpSession session) throws SQLException{	
		ArrayList<BeerInfo> list = service.searchBeerByMultipleCategory(value);
		ArrayList<BeerAssessment> list_as = common(session);
		map.addAttribute("result", list);
		map.addAttribute("result2", list_as);
		return "/beer/beer_search.jsp";
	}
	//달달한 맥주 검색
	@RequestMapping("/liquor/beer_search_sweetness.do")
	public String searchBeerBySweetness(ModelMap map, HttpSession session) throws SQLException{
		ArrayList<BeerInfo> list = service.searchBeerBySweetness();
		ArrayList<BeerAssessment> list_as = common(session);
		map.addAttribute("result", list);
		map.addAttribute("result2", list_as);
		return "/beer/beer_search.jsp";
	}
	//씁쓸한 맥주 검색
	@RequestMapping("/liquor/beer_search_bitterness.do")
	public String searchBeerByBitterness(ModelMap map, HttpSession session) throws SQLException{
		ArrayList<BeerInfo> list = service.searchBeerByBitterness();
		ArrayList<BeerAssessment> list_as = common(session);
		map.addAttribute("result", list);
		map.addAttribute("result2", list_as);
		return "/beer/beer_search.jsp";
	}
	//청량한 맥주 검색
	@RequestMapping("/liquor/beer_search_refreshment.do")
	public String searchBeerByRefreshment(ModelMap map, HttpSession session) throws SQLException{
		ArrayList<BeerInfo> list = service.searchBeerByRefreshment();
		ArrayList<BeerAssessment> list_as = common(session);
		map.addAttribute("result", list);
		map.addAttribute("result2", list_as);
		return "/beer/beer_search.jsp";
	}
	//흑맥주 검색
	@RequestMapping("/liquor/beer_search_blackbeer.do")
	public String searchBlackBeer(ModelMap map, HttpSession session) throws SQLException{
		ArrayList<BeerInfo> list = service.searchBlackBeer();
		ArrayList<BeerAssessment> list_as = common(session);
		map.addAttribute("result", list);
		map.addAttribute("result2", list_as);
		return "/beer/beer_search.jsp";
	}
	//낮은 도수 맥주 검색
	@RequestMapping("/liquor/beer_search_lowalcohol.do")
	public String searchLowAlcoholBeer(ModelMap map, HttpSession session) throws SQLException{
		ArrayList<BeerInfo> list = service.searchLowAlcoholBeer();
		ArrayList<BeerAssessment> list_as = common(session);
		map.addAttribute("result", list);
		map.addAttribute("result2", list_as);
		return "/beer/beer_search.jsp";
	}
	//높은 도수 맥주 검색
	@RequestMapping("/liquor/beer_search_highalcohol.do")
	public String searchHighAlcoholBeer(ModelMap map, HttpSession session) throws SQLException{
		ArrayList<BeerInfo> list = service.searchHighAlcoholBeer();
		ArrayList<BeerAssessment> list_as = common(session);
		map.addAttribute("result", list);
		map.addAttribute("result2", list_as);
		return "/beer/beer_search.jsp";
	}
	//무알콜 맥주 검색
	@RequestMapping("/liquor/beer_search_noalcohol.do")
	public String searchNoAlcoholBeer(ModelMap map, HttpSession session) throws SQLException{
		ArrayList<BeerInfo> list = service.searchNoAlcoholBeer();
		ArrayList<BeerAssessment> list_as = common(session);
		map.addAttribute("result", list);
		map.addAttribute("result2", list_as);
		return "/beer/beer_search.jsp";
	}
	
	
	//공통 메서드 - 회원별로 평가한 assessment 점수목록 list_as를 보내주기 위한 메서드
	public ArrayList<BeerAssessment> common(HttpSession session) throws SQLException{
		Member m = (Member)session.getAttribute("login_member");
		if(m!=null){ //session값이 있을 경우에만 list_as 리턴
			String memberId = m.getMemberId();
			ArrayList<BeerAssessment> list_as = service2.selectAssessmentByMemberId(memberId);
			return list_as;			
		}
		return null;
	}
	
}
